package com.atguigu.qqzone.controller;

import com.atguigu.qqzone.pojo.Topic;
import com.atguigu.qqzone.pojo.UserBasic;
import com.atguigu.qqzone.service.impl.TopicServiceImpl;
import com.atguigu.qqzone.service.impl.UserBasicServiceImpl;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: QqZoneController
 * @Description:
 * @Author: wty
 * @Date: 2022/12/9
 */

public class UserController {
    private UserBasicServiceImpl userBasicServiceImpl;
    private TopicServiceImpl topicServiceImpl;

    /**
     * @param
     * @return java.lang.String
     * @description 注册
     * @param: loginId
     * @param: pwd
     * @param: session
     * @date 2022/12/9 18:29
     * @author wty
     **/
    public String login(String loginId, String pwd, HttpSession session) {
        UserBasic userBasic = userBasicServiceImpl.login(loginId, pwd);
        if (null != userBasic) {
            // 获取好友列表
            List<UserBasic> friendList = userBasicServiceImpl.getFriendList(userBasic);
            // 获取日志列表
            List<Topic> topicList = topicServiceImpl.getTopicList(userBasic);

            userBasic.setFriendList(friendList);
            userBasic.setTopicList(topicList);

            // userBasic这个key保存的是登录者的信息
            session.setAttribute("userBasic", userBasic);
            // friend这个key保存的是登录者的信息
            session.setAttribute("friend", userBasic);
            // 验证成功,跳转主界面
            return "index";
        } else {
            // 验证失败,跳转注册界面
            return "login";
        }
    }


    public String friend(Integer id, HttpSession session) {
        // 1.根据id获取指定的用户信息
        UserBasic currendFriend = userBasicServiceImpl.getUserBasicById(id);

        if (null != currendFriend) {
            // 获取日志列表
            List<Topic> topicList = topicServiceImpl.getTopicList(currendFriend);

            currendFriend.setTopicList(topicList);

            // friend这个key保存的是登录者的信息
            session.setAttribute("friend", currendFriend);
            // 验证成功,跳转主界面
            return "index";
        } else {
            // 验证失败,跳转注册界面
            return "login";
        }
    }
}
